﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;


public partial class Admin_AdminCustomers : System.Web.UI.Page
{
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand cmd;
    private DataSet ds;
    private string connectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["myCon"].ToString();
        connect = new SqlConnection(connectionString);
        connect.Open();
        ds = new DataSet();
        if(!IsPostBack)
        {
            bindDataCustomer();
        }
    }

    protected void bindDataCustomer()
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetAllCustomer", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }

    protected void bindDataOrder(int id)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetAllOrderbyCustomer", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@cusID",SqlDbType.Int).Value = id;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            GridView2.DataSource = ds.Tables[0];
            GridView2.DataBind();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        int id = Int32.Parse(row.Cells[1].Text);
        bindDataOrder(id);
        GridView1.Visible = false;
        GridView2.Visible = true;
        btnBack.Visible = true;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        GridView1.Visible = true;
        GridView2.Visible = false;
        btnBack.Visible = false;
    }
}